package com.appcan.base;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public abstract class BaseBean<T> extends MerpBean implements BaseBo<T>{
	
	private static final Log log = LogFactory.getLog(BaseBean.class);

	private BaseDAO<T> dao = null;

	@PostConstruct
	public void init() {
		dao = new BaseDAO<T>(getManager(this.getClass()));
	}
	@Override
	public void insertBatchEntity(List<T> entry) throws BusinessException {
		dao.saveEntityList(entry);
	}
	@Override
	public void saveEntity(T entry) throws BusinessException {
		dao.saveEntry(entry);
		
	}
	@Override
	public void updateEntity(T entry) throws BusinessException {
		dao.updateEntry(entry);
		
	}
	@Override
	public List<Object[]> createNativeAquery(String sql)
			throws BusinessException {
		return dao.executeSql(sql);
	}
	@Override
	public List<T> qyeryEntityByHql(String hql) throws BusinessException {
		return dao.qyeryEntityByHql(hql);
	}
	@Override
	public int findListCount(Map<String, String> condition) throws BusinessException {
		int count = 0;
		try {
			String hql = this.getSql(condition, true);
			count = this.dao.getCountByHql(hql);
		}catch (Exception e) {
			log.error("获取数据总数异常." + e.toString());
			throw new BusinessException(MerpBean.class,"获取数据总数异常." + e.toString());
		}
		return count;
	}

	@Override
	public List<T> findList(int firstresult, int maxresult, Map<String, String> condition) throws BusinessException {
		List<T> list = null;
		try {
			String hql = this.getSql(condition, false);
			list = this.dao.getListByHql(firstresult, maxresult, hql);
		}
		catch (Exception e) {
			log.error("获取数据列表异常." + e.toString());
			throw new BusinessException(MerpBean.class,"获取数据列表异常." + e.toString());
		}
		return list;
	}

	@Override
	public List<Object[]> findNativeList(int firstresult, int maxresult, Map<String, String> condition) throws BusinessException {
		List<Object[]> list = null;
		try {
			String sql = this.getNativeSql(condition, false);
			list = this.dao.getNativeListBySql(firstresult, maxresult, sql);
		}
		catch (Exception e) {
			log.error("获取数据列表异常." + e.toString());
			throw new BusinessException(MerpBean.class,"获取数据列表异常." + e.toString());
		}
		return list;
	}
	
	@Override
	public int findNativeListCount(Map<String, String> condition) throws BusinessException {
		int count = 0;
		try {
			String sql = this.getNativeSql(condition, true);
			count = this.dao.getCountByNativeSql(sql);
		}
		catch (Exception e) {
			log.error("获取数据总数异常." + e.toString());
			throw new BusinessException(MerpBean.class,"获取数据列表异常." + e.toString());
		}
		return count;
	}
	
	@Override
	public T findEntityById(Map<String, String> conditions) throws BusinessException {
		T entity = null;
		try {
			String hql = this.getSql(conditions, false);
			entity = this.dao.getEntityByHql(hql);
		}
		catch (Exception e) {
			log.error("获取数据实体异常." + e.toString());
			throw new BusinessException(MerpBean.class,"获取数据实体异常." + e.toString());
		}
		return entity;
	}
	
	
	public void delEntity(T t) throws BusinessException {
		try {
			this.dao.delEntity(t);
		}
		catch (Exception e) {
			log.error("删除数据实体异常." + e.toString());
			throw new BusinessException(MerpBean.class,"删除数据实体异常." + e.toString());
		}		
	}
	
	public void deleteById(String tablename, String id) throws BusinessException {
		try {
			String sql = "delete from " + tablename + " where id=" + id;
			this.dao.executeUpdateBySql(sql);
		}
		catch (Exception e) {
			log.error("删除数据实体异常." + e.toString());
			throw new BusinessException(MerpBean.class,"删除数据实体异常." + e.toString());
		}
	}
	
	public void deleteBySql(String sql) throws BusinessException {
		try {
			this.dao.executeUpdateBySql(sql);
		}
		catch (Exception e) {
			log.error("删除数据实体异常." + e.toString());
			throw new BusinessException(MerpBean.class,"删除数据实体异常." + e.toString());
		}
	}
	
	public String findSingleValueListBySql(String sql) throws BusinessException {
		String singleValue = null;
		List<Object> list = this.dao.getNativeList(sql);
		if (list != null && list.size() > 0) {
			singleValue = (String)list.get(0);
		}
		return singleValue;
	}
	@Override
	public String getSql(Map<String, String> conditions, boolean isCount) {
		return null;
	}
	@Override
	public String getNativeSql(Map<String, String> conditions, boolean isCount) {
		return null;
	}
	@Override
	public Object quteryData(String billtype) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void updateData( Map map,String typeCode) throws BusinessException {
	
	}

}
